package com.qk.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qk.common.PageResult;
import com.qk.dto.BusinessDto;
import com.qk.entity.Business;
import com.qk.entity.BusinessTrackRecord;
import com.qk.entity.Customer;
import com.qk.mapper.BusinessMapper;
import com.qk.mapper.CustomerMapper;
import com.qk.mapper.businessTrackRecordMapper;
import com.qk.service.BusinessService;
import com.qk.utils.CurrentUserHoler;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class BusinessServiceImpl extends ServiceImpl<BusinessMapper, Business> implements BusinessService {
    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private businessTrackRecordMapper businessTrackRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public PageResult<Business> listBusiness(BusinessDto businessDto) {
        //查询列表商机
       Page<Business> page = businessMapper.listBusiness (new Page<Business>(businessDto.getPage(),businessDto.getPageSize()), businessDto);
       return new PageResult<>(page.getTotal(), page.getRecords());
    }
    //4. 根据ID查询商机
    @Override
    public Business getBusinessById(Integer id) {
        return businessMapper.getBusinessById(id);
    }
    //跟进商机
    @Transactional(rollbackFor = Exception.class)//添加事务控制
    @Override
    public void trackBusiness(Business business) {
        //1. 更新商机信息
        business.setStatus(3);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        //2. 添加商机跟踪记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord() ;
        businessTrackRecord.setBusinessId(business.getId());
        businessTrackRecord.setUserId(CurrentUserHoler.getCurrentUser());//跟进人 Id
        businessTrackRecord.setKeyItems(business.getKeyItems().toString());
        businessTrackRecord.setTrackStatus(business.getStatus());
        businessTrackRecord.setNextTime(business.getNextTime());
        businessTrackRecord.setRecord(business.getRecord());
        businessTrackRecord.setCreateTime(LocalDateTime.now());
        businessTrackRecordMapper.insert(businessTrackRecord);

    }
    // 转客户处理
    @Transactional(rollbackFor = Exception.class)//添加事务控制
    @Override
    public void toCustomer(Integer id) {
        //1.根据Id更新商机信息（转客户）
        Business business = businessMapper.selectById(id);
        business.setStatus(5);
        business.setUpdateTime(LocalDateTime.now());
        businessMapper.updateById(business);
        //2.保存客户信息（根据商机组装客户）
        Customer customer = new Customer();
        BeanUtils.copyProperties(business,customer);
        customer.setId(null);
        customer.setBusinessId(id);
        customer.setCreateTime(LocalDateTime.now());
        customer.setUpdateTime(LocalDateTime.now());
        customerMapper.insert(customer);
    }
    //8. 公海池列表查询
    @Override
    public PageResult<Business> listPool(BusinessDto businessDto) {
        Page<Business> page = businessMapper.listPools(new Page<Business>(businessDto.getPage(),businessDto.getPageSize()), businessDto);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }
}
